/*     */ package com.zimbra.cs.extension;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.redolog.op.RedoableOp;
/*     */ import java.io.File;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URI;
/*     */ import java.net.URL;
/*     */ import java.net.URLClassLoader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExtensionUtil
/*     */ {
/*  35 */   private static List<ZimbraExtensionClassLoader> sClassLoaders = new ArrayList();
/*     */   private static ClassLoader sExtParentClassLoader;
/*     */   
/*  38 */   public static URL[] dirListToURLs(File dir) { File[] files = dir.listFiles();
/*  39 */     if (files == null) {
/*  40 */       return null;
/*     */     }
/*  42 */     List<URL> urls = new ArrayList(files.length);
/*  43 */     for (int i = 0; i < files.length; i++) {
/*     */       try {
/*  45 */         URI uri = files[i].toURI();
/*  46 */         URL url = uri.toURL();
/*  47 */         urls.add(url);
/*  48 */         if (ZimbraLog.extensions.isDebugEnabled()) {
/*  49 */           ZimbraLog.extensions.debug("adding url: " + url);
/*     */         }
/*     */       } catch (MalformedURLException mue) {
/*  52 */         ZimbraLog.extensions.warn("ExtensionsUtil: exception creating url for " + files[i], mue);
/*     */       }
/*     */     }
/*  55 */     return (URL[])urls.toArray(new URL[0]);
/*     */   }
/*     */   
/*     */ 
/*     */   static
/*     */   {
/*  61 */     File extCommonDir = new File(LC.zimbra_extension_common_directory.value());
/*  62 */     URL[] extCommonURLs = dirListToURLs(extCommonDir);
/*  63 */     if (extCommonURLs == null)
/*     */     {
/*  65 */       sExtParentClassLoader = ExtensionUtil.class.getClassLoader();
/*     */     } else {
/*  67 */       sExtParentClassLoader = new URLClassLoader(extCommonURLs, ExtensionUtil.class.getClassLoader());
/*     */     }
/*  69 */     loadAll();
/*     */   }
/*     */   
/*     */   static synchronized void addClassLoader(ZimbraExtensionClassLoader zcl) {
/*  73 */     sClassLoaders.add(zcl);
/*     */   }
/*     */   
/*     */   private static synchronized void loadAll() {
/*  77 */     if (LC.zimbra_extension_directory.value() == null) {
/*  78 */       ZimbraLog.extensions.info(LC.zimbra_extension_directory.key() + " is null, no extensions loaded");
/*     */       
/*  80 */       return;
/*     */     }
/*  82 */     File extDir = new File(LC.zimbra_extension_directory.value());
/*  83 */     ZimbraLog.extensions.info("Loading extensions from " + extDir.getPath());
/*     */     
/*  85 */     File[] extDirs = extDir.listFiles();
/*  86 */     if (extDirs == null) {
/*  87 */       return;
/*     */     }
/*  89 */     for (File dir : extDirs)
/*  90 */       if (!dir.isDirectory()) {
/*  91 */         ZimbraLog.extensions.warn("ignored non-directory in extensions directory: " + dir);
/*     */       }
/*     */       else
/*     */       {
/*  95 */         ZimbraExtensionClassLoader zcl = new ZimbraExtensionClassLoader(dirListToURLs(dir), sExtParentClassLoader);
/*     */         
/*  97 */         if (!zcl.hasExtensions()) {
/*  98 */           ZimbraLog.extensions.warn("no Zimbra-Extension-Class found, ignored: " + dir);
/*     */         }
/*     */         else
/*     */         {
/* 102 */           sClassLoaders.add(zcl); }
/*     */       }
/*     */   }
/*     */   
/* 106 */   private static Map<String, ZimbraExtension> sInitializedExtensions = new LinkedHashMap();
/*     */   
/*     */   public static synchronized void initAll() {
/* 109 */     ZimbraLog.extensions.info("Initializing extensions");
/* 110 */     for (Iterator i$ = sClassLoaders.iterator(); i$.hasNext();) { zcl = (ZimbraExtensionClassLoader)i$.next();
/* 111 */       for (String name : zcl.getExtensionClassNames()) {
/*     */         try {
/* 113 */           Class<?> clazz = zcl.loadClass(name);
/* 114 */           ZimbraExtension ext = (ZimbraExtension)clazz.newInstance();
/*     */           try {
/* 116 */             ext.init();
/* 117 */             RedoableOp.registerClassLoader(ext.getClass().getClassLoader());
/* 118 */             String extName = ext.getName();
/* 119 */             ZimbraLog.extensions.info("Initialized extension " + extName + ": " + name + "@" + zcl);
/*     */             
/* 121 */             sInitializedExtensions.put(extName, ext);
/*     */           } catch (ExtensionException e) {
/* 123 */             ZimbraLog.extensions.info("Disabled '" + ext.getName() + "' " + e.getMessage());
/*     */             
/*     */ 
/* 126 */             ext.destroy();
/* 127 */             RedoableOp.deregisterClassLoader(ext.getClass().getClassLoader());
/*     */           }
/*     */           catch (Exception e) {
/* 130 */             ZimbraLog.extensions.warn("exception in " + name + ".init()", e);
/* 131 */             RedoableOp.deregisterClassLoader(ext.getClass().getClassLoader());
/*     */           }
/*     */         }
/*     */         catch (InstantiationException e) {
/* 135 */           ZimbraLog.extensions.warn("exception occurred initializing extension " + name, e);
/*     */         } catch (IllegalAccessException e) {
/* 137 */           ZimbraLog.extensions.warn("exception occurred initializing extension " + name, e);
/*     */         } catch (ClassNotFoundException e) {
/* 139 */           ZimbraLog.extensions.warn("exception occurred initializing extension " + name, e);
/*     */         }
/*     */       }
/*     */     }
/*     */     ZimbraExtensionClassLoader zcl;
/*     */   }
/*     */   
/*     */   public static synchronized void postInitAll() {
/* 147 */     ZimbraLog.extensions.info("Post-Initializing extensions");
/*     */     
/* 149 */     for (Object o : sInitializedExtensions.values()) {
/* 150 */       if ((o instanceof ZimbraExtensionPostInit)) {
/* 151 */         ((ZimbraExtensionPostInit)o).postInit();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized void destroyAll()
/*     */   {
/* 158 */     ZimbraLog.extensions.info("Destroying extensions");
/* 159 */     List<String> extNames = new ArrayList(sInitializedExtensions.keySet());
/* 160 */     for (String extName : extNames) {
/* 161 */       ZimbraExtension ext = getExtension(extName);
/*     */       try {
/* 163 */         RedoableOp.deregisterClassLoader(ext.getClass().getClassLoader());
/* 164 */         ext.destroy();
/* 165 */         ZimbraLog.extensions.info("Destroyed extension " + extName + ": " + ext.getClass().getName() + "@" + ext.getClass().getClassLoader());
/*     */       } catch (Exception e) {
/* 167 */         ZimbraLog.extensions.warn("exception in " + ext.getClass().getName() + ".destroy()", e);
/*     */       }
/*     */     }
/* 170 */     sInitializedExtensions.clear();
/*     */   }
/*     */   
/*     */   public static synchronized Class<?> loadClass(String extensionName, String className) throws ClassNotFoundException {
/* 174 */     if (extensionName == null) {
/* 175 */       return Class.forName(className);
/*     */     }
/* 177 */     ZimbraExtension ext = (ZimbraExtension)sInitializedExtensions.get(extensionName);
/* 178 */     if (ext == null) {
/* 179 */       throw new ClassNotFoundException("extension " + extensionName + " not found");
/*     */     }
/* 181 */     ClassLoader loader = ext.getClass().getClassLoader();
/* 182 */     return loader.loadClass(className);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static synchronized Class<?> findClass(String name)
/*     */     throws ClassNotFoundException
/*     */   {
/*     */     try
/*     */     {
/* 194 */       return ExtensionUtil.class.getClassLoader().loadClass(name);
/*     */     }
/*     */     catch (ClassNotFoundException e)
/*     */     {
/* 198 */       for (ZimbraExtensionClassLoader zcl : sClassLoaders) {
/*     */         try {
/* 200 */           return zcl.loadClass(name);
/*     */         }
/*     */         catch (ClassNotFoundException e) {}
/*     */       }
/*     */       
/* 205 */       throw new ClassNotFoundException(name);
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized ZimbraExtension getExtension(String name) {
/* 210 */     return (ZimbraExtension)sInitializedExtensions.get(name);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/extension/ExtensionUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */